<?php
/**
 * Sets the body-tag class attribute.
 *
 * Adds 'sidebar-left', 'sidebar-right' or 'sidebars' classes as needed.
 */
function phptemplate_body_class($sidebar_left, $sidebar_right) {
  if ($sidebar_left != '' && $sidebar_right != '') {
    $class = 'sidebars';
  }
  else {
    if ($sidebar_left != '') {
      $class = 'sidebar-left';
    }
    if ($sidebar_right != '') {
      $class = 'sidebar-right';
    }
  }

  if (isset($class)) {
    print ' class="'. $class .'"';
  }
}

/**
 * Return a themed breadcrumb trail.
 *
 * @param $breadcrumb
 *   An array containing the breadcrumb links.
 * @return a string containing the breadcrumb output.
 */
function phptemplate_breadcrumb($breadcrumb) {
  if (!empty($breadcrumb)) {
    return '<div class="breadcrumb">'. implode(' › ', $breadcrumb) .'</div>';
  }
}

/**
 * Allow themable wrapping of all comments.
 */
function phptemplate_comment_wrapper($content, $type = null) {
  static $node_type;
  if (isset($type)) $node_type = $type;

  if (!$content || $node_type == 'forum') {
    return '<div id="comments">'. $content . '</div>';
  }
  else {
    return '<div id="comments"><h2 class="comments">'. t('Comments') .'</h2>'. $content .'</div>';
  }
}

/**
 * Override or insert PHPTemplate variables into the templates.
 */
function _phptemplate_variables($hook, $vars) {
  if ($hook == 'page') {

    if ($secondary = menu_secondary_local_tasks()) {
      $output = '<span class="clear"></span>';
      $output .= "<ul class=\"tabs secondary\">\n". $secondary ."</ul>\n";
      $vars['tabs2'] = $output;
    }

    // Hook into color.module
    if (module_exists('color')) {
      _color_page_alter($vars);
    }
    return $vars;
  }
  return array();
}

/**
 * Returns the rendered local tasks. The default implementation renders
 * them as tabs.
 *
 * @ingroup themeable
 */
function phptemplate_menu_local_tasks() {
  $output = '';

  if ($primary = menu_primary_local_tasks()) {
    $output .= "<ul class=\"tabs primary\">\n". $primary ."</ul>\n";
  }

  return $output;
}

function aberdeen_shva_theme($existing, $type, $theme, $path) {
  return array(
    'shva_links' => array(
      'arguments' => array('links' => NULL, 'attributes' => NULL),
    ),
  );
}


/**
 * Override theme_links to include <span> in list.
 */
function aberdeen_shva_shva_links($links, $attributes = array('class' => 'links')) {
	$output = '';

  if (count($links) > 0) {
    $output = '<ul'. drupal_attributes($attributes) .'>';
    
    $output .= '<li>Друзья: </li>';

    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      $class = $key;

      // Automatically add a class to each link and also to each LI
      if (isset($link['attributes']) && isset($link['attributes']['class'])) {
        $link['attributes']['class'] .= ' ' . $key;
      }
      else {
        $link['attributes']['class'] = $key;
      }

      // Add first and last classes to the list of links to help out themers.
      $extra_class = '';
      if ($i == 1) {
        $extra_class .= 'first ';
      }
      if ($i == $num_links) {
        $extra_class .= 'last ';
      }
      $output .= '<li '. drupal_attributes(array('class' => $extra_class . $class)) .'>';

      // Is the title HTML?
      $html = isset($link['html']) && $link['html'];

      // Initialize fragment and query variables.
      $link['query'] = isset($link['query']) ? $link['query'] : NULL;
      $link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;

      if (isset($link['href'])) {
        $output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
      }
      else if ($link['title']) {
        //Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (!$html) {
          $link['title'] = check_plain($link['title']);
        }
        $output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
      }

      $i++;
      $output .= "</li> |\n";
    }

    $output .= '</ul>';
  }

  return $output;
}


// Спартак — Чемпион

function theme_views_view_live($view, $type, $nodes){
	$output = "";
	for($i = 0; $i <= count($nodes) - 1; $i++){
		if(module_exists("og")) {
			$r = db_fetch_object(db_query("SELECT `group_nid` AS nid FROM {og_ancestry} WHERE `nid` = %d LIMIT 0,1", $nodes[$i]->nid));
			$x = db_fetch_object(db_query("SELECT `title`, `nid` FROM {node} WHERE `nid` = %d", $r->nid));
			
			$group = "";		
			if($x->title) {
				$group_link = "node/" . $x->nid;
				$group_link = drupal_get_path_alias($group_link);
				$group = "<a href=\"" . base_path() . $group_link . "\">" . $x->title . "</a> → ";
				
			}
		} else {
			$group = "";
		}
		
		$user_name = "";
		if($nodes[$i]->comments_uid) {
			$user_name = "<a href=\"" . base_path() . "user/" . $nodes[$i]->comments_uid . "\">" . $nodes[$i]->comments_name . "</a> → ";
		} else {
			$user_name = $nodes[$i]->comments_name . " → ";
		}
		
		$node_path = "node/" . $nodes[$i]->nid;
		$node_path = drupal_get_path_alias($node_path);
		
		$output .= $user_name . $group .  "<a href=\"" . base_path() . $node_path . "#comment-" . $nodes[$i]->comments_cid . "\">" . $nodes[$i]->node_title . "</a><br>";
	}		
	return $output;
}

function aberdeen_vote_up_down_widget($cid, $type) {
  global $user;  

  if (user_access('view up-down vote')) {
  	
  	$b = TRUE;
  	if($type == 'node') $r = db_fetch_object(db_query("SELECT `uid` FROM {node} WHERE nid = %d LIMIT 0,1", $cid));
  	if($type == 'comment') $r = db_fetch_object(db_query("SELECT `uid` FROM {comments} WHERE cid = %d LIMIT 0,1", $cid));

 		if($user->uid == $r->uid) $b = FALSE;

    $output = '<div class="vote-up-down-widget">';

    if (user_access('use up-down vote') && ($user->uid || variable_get('vote_up_down_anonymous_vote', 0)) && $b) {
      $user_vote = votingapi_get_user_votes($type, $cid, _vote_up_down_get_uid());

      if ($user_vote[0]->value > 0) {
        $class = 'vote-up-act';
        $class2 = 'vote-down-inact';
      }
      else if ($user_vote[0]->value < 0) {
        $class = 'vote-up-inact';
        $class2 = 'vote-down-act';
      }
      else {
        $class = 'vote-up-inact';
        $class2 = 'vote-down-inact';
      }

      $output .= '<span id="vote_up_'. $cid .'" class="'. $class .'" title="'. url("vote_up_down/$type/$cid/1/1") .'">'. l('', "vote_up_down/$type/$cid/1", array('class' => $class, 'title' => t('Vote up')), drupal_get_destination(), NULL, FALSE, TRUE) .'</span>';
      $output .= '<span id="vote_down_'. $cid .'" class="'. $class2 .'" title="'. url("vote_up_down/$type/$cid/-1/1") .'">'. l('', "vote_up_down/$type/$cid/-1", array('class' => $class2, 'title' => t('Vote down')), drupal_get_destination(), NULL, FALSE, TRUE) .'</span>';
    }
    else if(!$b) {
      $output .= '<span class="up-inact" title="'. t('Вы не можете голосовать за свой материал.') .'"></span>';
      $output .= '<span class="down-inact" title="'. t('Вы не можете голосовать за свой материал.') .'"></span>';
    } else {
      $output .= '<span class="up-inact" title="'. t('You must login to vote.') .'"></span>';
      $output .= '<span class="down-inact" title="'. t('You must login to vote.') .'"></span>';
    }

    $output .= '</div>';

    return $output;
  }
}

function shvabr_get_user_info($uid) {
	$info = array();
	$q = db_query("SELECT g.message, g.author, u.name FROM {guestbook} g INNER JOIN {users} u ON g.author = u.uid WHERE g.recipient = %d", $uid);
	while($r = db_fetch_object($q)) {
		$info['guestbook'][] = array(
			'author' => $r->author,
			'name' => $r->name,
			'message' => $r->message,
		);
		
	}
	$q = db_query("SELECT n.nid, n.title FROM {node} n WHERE n.type = 'personal_gallery' AND n.uid = %d", $uid);
	while($r = db_fetch_object($q)) {
		$info['personal_galleries'][] = array(
			'nid' => $r->nid,
			'title' => $r->title,
		);

		$photos = db_query("SELECT DISTINCT f.filepath, i.nid, n.title FROM {content_type_image} i INNER JOIN {node} n ON n.nid = i.nid INNER JOIN {files} f ON f.fid = i.field_image_image_fid WHERE i.field_image_gallery_nid = %d LIMIT 0,4", $r->nid);
		while($photo = db_fetch_object($photos)) {
			$info['photos'][$r->nid][] = array(
			  'nid' => $photo->nid,
				'title' => $photo->title,
				'filepath' => $photo->filepath,
			);
		}
	}
	
	$info["is_microblog"] = FALSE;
	$r = db_fetch_object(db_query("SELECT COUNT(*) AS count FROM {facebook_status} WHERE uid = %d", $uid));
	if($r->count > 0) {
		$info["is_microblog"] = TRUE;
	}
	
	$q = db_query("SELECT DISTINCT ur.requester_id AS uid FROM {user_relationships} ur WHERE ur.requestee_id = %d AND ur.approved = 1", $uid); 	
	$info["friends"] = array();
	while($r = db_fetch_object($q)) {
		$info["friends"][] = user_load($r->uid);
	}
	return $info;
}

function aberdeen_shva_user_visits($account, $timestamp = NULL, $referer = NULL) {
	//fe($account);
	$output  = '<div>'."\n";
  if(isset($account->picture) && trim($account->picture) != "") {
  	$name = "";
  	if ($timestamp) {
  		$name .= $account->name . ": ";
  		$name .= t('!time ago', array('!time' => format_interval(time() - $timestamp)));  		
    }
  	$output .= '<div style="float: left; margin: 3px 3px 0 0;"><a href="' . drupal_get_path_alias("user/" . $account->uid) . '"><img src="' . base_path() . $account->picture . '" alt="' . $name . '" title="' . $name . '"></a></div>';
  } else {
  	$output .= ' <div class="name">'. theme('username', $account) .'</div>'."\n";
  }
  return $output;
}

function shvabra_image_gallery_pager($nid) {
	$r = db_fetch_object(db_query("SELECT field_image_gallery_nid AS gal FROM {content_type_image} WHERE nid = %d", $nid));
	if($r->gal) {
		$gal = $r->gal;
		$output = "";
		$prev = "";
		$next = "";
		$r = db_fetch_object(db_query("SELECT c.nid, f.filepath, n.title FROM {content_type_image} c INNER JOIN {files} f ON f.fid = c.field_image_image_fid INNER JOIN {node} n ON n.nid = c.nid  WHERE c.nid > %d AND c.field_image_gallery_nid = %d ORDER BY nid ASC LIMIT 0,1", $nid, $gal));
		if($r->nid) {
			$next = '<a href="' . base_path() . drupal_get_path_alias("node/" . $r->nid) . '">' . theme('imagecache', 'thumbnail', $r->filepath, $r->title, $r->title) . '</a>';
		}
		
		$r = db_fetch_object(db_query("SELECT c.nid, f.filepath, n.title FROM {content_type_image} c INNER JOIN {files} f ON f.fid = c.field_image_image_fid INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid < %d AND c.field_image_gallery_nid = %d ORDER BY nid DESC LIMIT 0,1", $nid, $gal));
		if($r->nid) {
			$prev = '<a href="' . base_path() . drupal_get_path_alias("node/" . $r->nid) . '">' . theme('imagecache', 'thumbnail', $r->filepath, $r->title, $r->title) . '</a>';
		}
		
		$output .= '<div class="field-label"><strong>Навигация по галерее: </strong></div>' . $prev . " " . $next;
		
		return $output;
	}
	
	return "";	
	
}
?>